// pages/Address/edit.js
const Controller = require('../controllers/controllers.js').controller;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navigabarTitle: '地址',
        type: '',
        checked: true,
        show: false,
        region: [],
        addressId: '',
        loading:false,
        from: {
            name: "",
            mobile: "",
            province: "",
            city: "",
            area: "",
            address: "",
            isDefault: 0
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.type == 'add') {
            this.setData({
                navigabarTitle: '新增收货地址',
                type: options.type
            })
        } else {
            this.setData({
                navigabarTitle: '编辑收货地址',
                type: options.type
            })
            if (options.id) {
                this.setData({
                    addressId: options.id
                })
            }
            this.getInfo()
        }
    },

    //获取详情
    getInfo() {
        Controller.addressDetail({
            addressId: this.data.addressId
        }).then(res => {
            let from = res.data.baseData
            let region = [from.province, from.city, from.area]
            this.setData({
                from,
                region
            })
        })
    },

    //保存
    save() {
        let from = this.data.from
        let region = this.data.region
        if (!from.name) {
            wx.showToast({
                title: '姓名不能为空',
                icon: 'none'
            })
            return
        }
        if (!/^1[0-9]{10}$/.test(from.mobile)) {
            wx.showToast({
                title: '请输入正确的手机号',
                icon: 'none'
            })
            return
        }
        if (region.length == 0) {
            wx.showToast({
                title: '请先选择地区',
                icon: 'none'
            })
            return
        }
        if (!from.address) {
            wx.showToast({
                title: '地址不能为空',
                icon: 'none'
            })
            return
        }
        from.province = region[0]
        from.city = region[1]
        from.area = region[2]
        this.setData({
            loading:true
        })
        if (this.data.type == "add") {
            
            Controller.addAddress(from).then(res => {
                
              
            if(res.code==0){
                wx.showToast({
                    title: '新增成功',
                    icon:'none'
                })
                setTimeout(() => {
                    this.setData({
                        loading:false
                    })
                    wx.navigateBack()
                }, 100)

            }
               
            }).catch(err=>{
                console.log('err',err)
                this.setData({
                    loading:false
                })

            })
        } else {
            from.addressId = this.data.addressId
            Controller.editAddress(from).then(res => {

                if(res.code==0){
                    wx.showToast({
                        title: '编辑成功'
                    })
                    setTimeout(() => {
                        this.setData({
                            loading:false
                        })
                        wx.navigateBack()
                    }, 100)

                }
               
            }).catch(err=>{
                console.log('err',err)
                 this.setData({
                     loading:false
                 })
            })
        }
    },

    //输入
    input(e) {
        let name = e.currentTarget.dataset.name
        let from = this.data.from
        from[name] = e.detail.value
        this.setData({
            from
        });
    },
    onChange(e){
     console.log('e==>',e)
     this.setData({
         'from.isDefault':e.detail?1:0
     })
    },
    //Picker改变事件
    bindRegionChange(e) {
        this.setData({
            region: e.detail.value,
        })
    },



    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})